const {
  $util,
} = getApp();
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    gid: '',
    type: 0,
    hostname: '',
    title: '',
    content: '',
    timelimit: [],
    place: '请选择会议地点',
    map: '',
    latitude: '',
    longitude: '',
    signdistance: '',
    opendelay: 0,
    thumb: '',
    enabled: 0,
    target_org: '',
    target_obj: '', // 党员id
    meetingTypes: [], // 会议类型
    nowDate: '',
    startDate: '',
    endDate: '',
    startTime: '',
    endTime: '',
    isLoading: false,
    idx: 0,
    imgUrl: $util.getImgUrl(),
  },
  startDateChange: function (e) {
    // 选择开始日期的时候 将结束日期时间默认为同一天
    this.setData({
      startDate: e.detail.value,
    });

    let _startTime = this.data.startDate + ' ' + this.data.startTime;
    this.setData({
      endDate: getApp().util.formateDateTime((new Date(_startTime).getTime() + 60 * 60 * 1000) / 1000),
      endTime: getApp().util.format(new Date(_startTime).getTime() + 60 * 60 * 1000, 'hh:mm'),
    });
    wx.setStorageSync('mettingTime', {
      endDate: this.data.endDate,
      endTime: this.data.endTime,
      startDate: this.data.startDate,
      startTime: this.data.startTime,
    });
  },
  endDateChange: function (e) {
    // 选择开始时间的时候 将结束日期时间默认延迟一小时
    this.setData({
      endDate: e.detail.value,
    });
    wx.setStorageSync('mettingTime', {
      endDate: this.data.endDate,
      endTime: this.data.endTime,
      startDate: this.data.startDate,
      startTime: this.data.startTime,
    });
  },
  startTimeChange(e) {
    this.setData({
      startTime: e.detail.value,
    });

    let _startTime = this.data.startDate + ' ' + this.data.startTime;
    this.setData({
      endDate: getApp().util.formateDateTime((new Date(_startTime).getTime() + 60 * 60 * 1000) / 1000),
      endTime: getApp().util.format(new Date(_startTime).getTime() + 60 * 60 * 1000, 'hh:mm'),
    });

    wx.setStorageSync('mettingTime', {
      endDate: this.data.endDate,
      endTime: this.data.endTime,
      startDate: this.data.startDate,
      startTime: this.data.startTime,
    });

  },
  endTimeChange(e) {
    this.setData({
      endTime: e.detail.value,
    });
    wx.setStorageSync('mettingTime', {
      endDate: this.data.endDate,
      endTime: this.data.endTime,
      startDate: this.data.startDate,
      startTime: this.data.startTime,
    });
  },
  bindKeyInput: function (e) {
    let name = e.currentTarget.dataset.flag;
    if (name === 'hostname') {
      this.setData({
        hostname: e.detail.value,
      });
    }
    if (name === 'title') {
      this.setData({
        title: e.detail.value,
      });
    }
    if (name === 'content') {
      this.setData({
        content: e.detail.value,
      });
    }
    if (name === 'signdistance') {
      this.setData({
        signdistance: e.detail.value,
      });
    }
  },
  getAddress: function () {
    let that = this;
    wx.chooseLocation({
      type: 'gcj02', // 返回可以用于wx.openLocation的经纬度
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          place: res.address,
          map: res.latitude + ',' + res.longitude,
        });
      },
      fail(res) {
        that.getSetting();
      },
    });
  },
  getSetting() {
    let that = this;
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.userLocation']) {
          wx.openSetting({
            success: (res) => {
              if (res.authSetting['scope.userLocation']) {
                that.getAddress();
              }
            },
          });
        }
      },
    });
  },
  bindTypeChange: function (e) {
    let typ = this.data.meetingTypes;
    this.setData({
      idx: e.detail.value,
      type: typ[e.detail.value].id,
    });
  },
  signRadioChange(e) {
    this.setData({
      opendelay: e.detail.value ? 1 : 0,
    });
  },
  statusRadioChange(e) {
    this.setData({
      enabled: e.detail.value ? 1 : 0,
    });
  },
  imgsOnChange(e) {
    this.setData({
      thumb: e.detail.length > 0 ? e.detail[0] : '',
    });
  },
  // 创建会议
  createMetting: function () {
    this.setData({
      isLoading: true,
    });
    const {
      type,
      hostname,
      title,
      content,
      place,
      map,
      signdistance,
      opendelay,
      thumb,
      enabled,
      target_org,
      target_obj,
    } = this.data;
    let that = this;
    if (hostname.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请填写主持人姓名',
        showCancel: false,
      });
      return;
    }
    if (title.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请填写会议标题',
        showCancel: false,
      });
      return;
    }
    if (content.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请填写议程',
        showCancel: false,
      });
      return;
    }
    if (map.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请选择参会地点',
        showCancel: false,
      });
      return;
    }
    if (target_obj.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请选择参会党员',
        showCancel: false,
      });
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确定要添加该会议吗？',
      success: (res) => {
        if (res.confirm) {
          $util.request({
            url: 'entry/wxapp/partylife.sessions.addmeeting',
            data: {
              type,
              hostname,
              title,
              content,
              timelimit_start: this.data.startDate + ' ' + this.data.startTime,
              timelimit_end: this.data.endDate + ' ' + this.data.endTime,
              place,
              map,
              signdistance,
              opendelay,
              thumb,
              enabled,
              target_org,
              target_obj,
            },
            success: (res) => {
              wx.removeStorageSync('addmeeting');
              wx.removeStorageSync('memberids');
              wx.removeStorageSync('orgids');
              wx.removeStorageSync('mettype');
              wx.navigateBack({});
            },
            complete: function () {
              that.setData({
                isLoading: false,
              });
            },
          });
        }
      },
    });
    
  },
  // 会议类型
  meetingType: function () {
    let typ = wx.getStorageSync('mettype') || [];
    this.setData({
      meetingTypes: typ,
      type: typ[0].id,
    });
  },
  bindOrgInput: function () {
    // 选择参会组织
    wx.navigateTo({
      url: '/ws_party_const/pages/organization/list',
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.meetingType();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
    let val = wx.getStorageSync('mettingTime');
    this.setData({
      nowDate: getApp().util.formateDateTime(new Date().getTime() / 1000),
      startDate: val.startDate || getApp().util.formateDateTime(new Date().getTime() / 1000),
      endDate: val.endDate || getApp().util.formateDateTime(new Date().getTime() / 1000),
      startTime: val.startTime || getApp().util.format(new Date(), 'hh:mm'),
      endTime: val.endTime || getApp().util.format(new Date().getTime() + 60 * 60 * 1000, 'hh:mm'),
    });

    // 获取选择的党员缓存信息
    if (wx.getStorageSync('addmeeting')) {
      let members = wx.getStorageSync('addmeeting');
      let ids = [];
      for (let i = 0; i < members.length; i++) {
        ids[i] = members[i].inid;
      }

      this.setData({
        members: members,
        target_obj: ids.join(','),
      });

    }

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onUnload: function () {
    wx.removeStorageSync('mettingTime');
  },
});